#include "doge-cmd-parser.h"

static int _is_blank(char c)
{
	if (c == ' ')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

static int _str_cmp(char *str1, char *str2)
{
	while (*str1 != '\0' && *str1 == *str2)
	{
		str1++;
		str2++;
	}

	int t = *str1 - *str2;
	if (t == 0) return 0;
	else if (t > 0) return 1;
	else return -1;
}

void doge_cmd_parse(char *str, doge_cmd_t *cmds, int cmds_len)
{
	char arg[DOGE_CMDS_MAX][DOGE_CMD_MAX_LEN] = {0};
    char *cmd[DOGE_CMDS_MAX];
	int argc = 0, index = 0, n = 0;

	for (n = 0; i < DOGE_CMDS_MAX; n++)
    {
        cmd[n] = arg[n];
    }

	char *pStr = (char *)str;

	while (_is_blank(*str))    /* Clear leading space */ 
	{
		str++;
	}

	argc++;
	n = 0;
	while (*str != '\0')
	{
		if (_is_blank(*str))  /* Skip Spaces */
		{
			str++;
			index++;
			n = 0;
			argc++;
		}
        if (index >= DOGE_CMDS_MAX)
            break;

		arg[index][n++] = *str;
		str++;
	}
//	printf("index = %d, arg[0] = %s arg[1] = %s, arg[2] = %s\r\n",  index, arg[0], arg[1], arg[2]);
	if (!lists) return;

//	printf("argc = %d list_len = %d\r\n", argc, list_len);
	for (n = 0; n < cmds_len; n++)
	{
		if (_str_cmp(arg[0], cmds[n].cmd) == 0)
		{
			if (!cmds[n].cb) return;

			cmds[n].cb(argc, cmd);
		}
	}
}
